//
//  DrawAddressViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/11.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "DrawAddressViewController.h"
#import "AddressCell.h"
#import "DrawAddressModel.h"
#import "DetailedAddressViewController.h"
@interface DrawAddressViewController ()
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation DrawAddressViewController
- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [self requstData];
}

- (void)requstData{
    
    [LSRequestManager getRequestWithURL:HUOBI(@"api/currency/addrCurrency")
                                  params:nil
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         
                                         [self.dataSource removeAllObjects];
                                         
                                         NSArray *array = [DrawAddressModel mj_objectArrayWithKeyValuesArray:responseData[@"data"]];
                                         
                                         [self.dataSource addObjectsFromArray:array];
                                         [self.tableView reloadData];
                                         
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
   return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[AddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddressCell"];
    }
    DrawAddressModel*drawAddressModel=self.dataSource[indexPath.row];
    cell.nameLabel.text=drawAddressModel.mark;
    cell.countLabel.text=drawAddressModel.total;
    
    return cell;
    
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    DrawAddressModel*drawAddressModel=self.dataSource[indexPath.row];
    [self performSegueWithIdentifier:@"detailedAddressSegue" sender:drawAddressModel];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"detailedAddressSegue"]){
        DetailedAddressViewController *detailCtrl = segue.destinationViewController;
        DrawAddressModel*drawAddressModel = sender;
        detailCtrl.mark=drawAddressModel.mark;
        detailCtrl.currency_id=drawAddressModel.currency_id;
    }
    
}



@end
